package com.qkl.xiatang.app.web.controller;

import com.qkl.xiatang.app.dto.BusinessDTO;
import com.qkl.xiatang.app.dto.user.UserOutDTO;
import com.qkl.xiatang.app.dto.user.UserRegisterInDTO;
import com.qkl.xiatang.app.service.impl.AppUserInfoService;
import com.qkl.xiatang.app.utils.ErrorCodeUtils;
import com.qkl.xiatang.app.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("{app.baseURL}")
@Slf4j
public class ApplicationUserController {

    @Autowired
    private AppUserInfoService userInfoService;


    /**
     * 用户注册
     * @param registerDTO
     * @return
     */
    @PostMapping("/userRegister")
    public Result userRegister(@RequestBody UserRegisterInDTO registerDTO) {
        String mobile = StringUtils.trimToEmpty(registerDTO.getMobile());
        String pwd = StringUtils.trimToEmpty(registerDTO.getPwd());
        String verifyCode = StringUtils.trimToEmpty(registerDTO.getVerifyCode());
        String companyId = StringUtils.trimToEmpty(registerDTO.getCompanyId());
        String name = StringUtils.trimToEmpty(registerDTO.getName());
        log.info("=== userRegister.mobile={},companyId={},name={}",mobile,companyId,name);
        if (StringUtils.isAnyEmpty(mobile,pwd,verifyCode,name)) {
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        UserRegisterInDTO inDTO = new UserRegisterInDTO();
        inDTO.setPwd(pwd);
        inDTO.setMobile(mobile);
        inDTO.setVerifyCode(verifyCode);
        inDTO.setCompanyId(companyId);
        inDTO.setName(name);
        BusinessDTO<Object>  businessDTO  = userInfoService.userRegister(inDTO);
        if(!businessDTO.isSucess()){
            return Result.error(ErrorCodeUtils.REGISTER_FAIL,businessDTO.getMessage());
        }
        return Result.success(businessDTO.getBusinessObject(),businessDTO.getMessage());
    }

    /**
     * 注消用户
     * @return
     */
    @PostMapping("/userCancel")
    public Result userCancel() {
        return ControllerHelper.toOperateResult(userInfoService.userCancel());
    }


    /**
     * 用户登陆,密码
     * @param params
     * @return
     */
    @PostMapping("/userPwdLogin")
    public Result userPwdLogin(@RequestBody Map<String,String> params) {
        String account = StringUtils.trimToEmpty(params.get("account"));
        String pwd = StringUtils.trimToEmpty(params.get("pwd"));
        String deviceId = StringUtils.trimToEmpty(params.get("deviceId"));
        if (StringUtils.isAnyEmpty(account,pwd)) {
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        BusinessDTO<UserOutDTO>  businessDTO = userInfoService.userPwdLogin(account,pwd,deviceId);
        if(!businessDTO.isSucess()){
            return Result.error(ErrorCodeUtils.LOGIN_FAIL,businessDTO.getMessage());
        }
        return Result.success(businessDTO.getBusinessObject(),businessDTO.getMessage());
    }

    /**
     * 用户登陆,手机验证码
     * @param params
     * @return
     */
    @PostMapping("/userVerifyLogin")
    public Result userVerifyLogin(@RequestBody Map<String,String> params) {
        String mobile = StringUtils.trimToEmpty(params.get("mobile"));
        String verifyCode = StringUtils.trimToEmpty(params.get("verifyCode"));
        String deviceId = StringUtils.trimToEmpty(params.get("deviceId"));
        if (StringUtils.isAnyEmpty(mobile,verifyCode)) {
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        BusinessDTO<UserOutDTO>  businessDTO = userInfoService.userVerifyLogin(mobile,verifyCode,deviceId);
        if(!businessDTO.isSucess()){
            return Result.error(ErrorCodeUtils.LOGIN_FAIL,businessDTO.getMessage());
        }
        return Result.success(businessDTO.getBusinessObject(),businessDTO.getMessage());
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/getAppUserInfo")
    public Result getAppUserInfo() {
        return ControllerHelper.toQueryResult(userInfoService.getAppUserInfo());
    }

    /**
     * 忘记密码重置
     * @param params
     * @return
     */
    @PostMapping("/resetPwd")
    public Result resetPwd(@RequestBody Map<String,String> params) {
        String mobile = StringUtils.trimToEmpty(params.get("mobile"));
        String verifyCode = StringUtils.trimToEmpty(params.get("verifyCode"));
        String pwd = StringUtils.trimToEmpty(params.get("pwd"));
        if (StringUtils.isAnyEmpty(mobile,verifyCode,pwd)) {
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        BusinessDTO<Object>  businessDTO = userInfoService.resetPwd(mobile,verifyCode,pwd);
        if(!businessDTO.isSucess()){
            return Result.error(ErrorCodeUtils.RESET_PWD_FAIL,businessDTO.getMessage());
        }
        return Result.success(businessDTO.getBusinessObject(),businessDTO.getMessage());
    }

    /**
     * 退出登陆
     * @return
     */
    @PostMapping("/userLogout")
    public Result userLogout() {
        BusinessDTO<Object>  businessDTO = userInfoService.userLogout();
        if(!businessDTO.isSucess()){
            return Result.error(ErrorCodeUtils.USER_LOGOUT_FAIL,businessDTO.getMessage());
        }
        return Result.success(businessDTO.getBusinessObject(),businessDTO.getMessage());
    }

    /**
     * 设置中心
     * @param params
     * @return
     */
    @PostMapping("/setUserInfo")
    public Result setUserInfo(@RequestBody Map<String,String> params) {
        String mobile = StringUtils.trimToEmpty(params.get("mobile"));
        String verifyCode = StringUtils.trimToEmpty(params.get("verifyCode"));
        String name = StringUtils.trimToEmpty(params.get("name"));
        String avatar = StringUtils.trimToEmpty(params.get("avatar"));
        if (StringUtils.isAllEmpty(mobile,name,avatar) || (StringUtils.isNotEmpty(mobile) && StringUtils.isEmpty(verifyCode))) {
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        BusinessDTO<Object>  businessDTO = userInfoService.setUserInfo(name,mobile,avatar,verifyCode);
        if(!businessDTO.isSucess()){
            return Result.error(ErrorCodeUtils.SET_USER_INFO_FAIL,businessDTO.getMessage());
        }
        return Result.success(businessDTO.getBusinessObject(),businessDTO.getMessage());
    }

    /**
     * 查询用户公司
     * @return
     */
    @GetMapping("/queryUserCompany")
    public Result queryUserCompany(@RequestParam Map<String,Object> params){
        String keywords = StringUtils.trimToEmpty((String) params.get("keywords"));
        Map<String,Object> map = new HashMap<>();
        map.put("companyList",userInfoService.queryUserCompanyInfo(keywords));
        return Result.success(map,"查询用户公司");
    }


    /**
     * 查询公司列表
     * @return
     */
    @GetMapping("/queryCompanyList")
    public Result queryCompanyList(@RequestParam Map<String,String> params){
        String keywords = StringUtils.trimToEmpty(params.get("keywords"));
        return Result.success(userInfoService.queryCompanyList(keywords),"查询公司列表");
    }

    /**
     * 切换用户公司
     * @param params
     * @return
     */
    @PostMapping("/switchUserCompany")
    public Result switchUserCompany(@RequestBody Map<String,String> params) {
        String companyId = StringUtils.trimToEmpty(params.get("companyId"));
        if(StringUtils.isEmpty(companyId)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        return ControllerHelper.toOperateResult(userInfoService.switchUserCompany(companyId));
    }

    /**
     * 用户附加信息
     * @param params
     * @return
     */
    @PostMapping("/userAdditInfo")
    public Result userAdditInfo(@RequestBody Map<String,Object> params) {
        return ControllerHelper.toOperateResult(userInfoService.userAdditInfo(params));
    }


}
